﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LingShi.Helpers;
using System.Data;
using LingShi.DAL;
using LingShi.Common;

namespace LingShi.Handler
{
    /// <summary>
    /// Summary description for ChinaAreaHandler
    /// </summary>
    public class ChinaAreaHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string returnValue = "[]";
            if (context.Request.QueryString["flag"] == "provice")
            {
                using (DBContext db = new DBContext())
                {
                    DataTable dt = new DataTable();
                    SqlResult result = db.CallProcedure("usp_provice_get", null);
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        dt = result.DataSet.Tables[0];
                    }
                    returnValue = "[";
                    foreach (DataRow dr in dt.Rows)
                    {
                        Province province = new Province { ProvinceID = MyUtils.ParseToInt(dr["id"], 0), ProvinceName = MyUtils.ParseToString(dr["provincename"], string.Empty) };
                        returnValue += UtilityMethod.ToJson(province)+",";
                    }
                    if (returnValue.Length > 1)
                        returnValue = returnValue.Substring(0, returnValue.Length - 1);
                    returnValue += "]";
                }
            }
            else if (context.Request.QueryString["flag"] == "city")
            {
                int provinceId = MyUtils.ParseToInt(context.Request.QueryString["province"], 0);
                if (provinceId != 0)
                {
                    using (DBContext db = new DBContext())
                    {
                        DataTable dt = new DataTable();
                        SqlResult result = db.CallProcedure("usp_cities_get_by_provice", provinceId);
                        if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                        {
                            dt = result.DataSet.Tables[0];
                        }
                        returnValue = "[";
                        foreach (DataRow dr in dt.Rows)
                        {
                            City city = new City { CityID = MyUtils.ParseToInt(dr["id"], 0), CityName = MyUtils.ParseToString(dr["cityname"], string.Empty) };
                            returnValue += UtilityMethod.ToJson(city) + ",";
                        }
                        if (returnValue.Length > 1)
                            returnValue = returnValue.Substring(0, returnValue.Length - 1);
                        returnValue += "]";
                    }
                }
            }
            else if (context.Request.QueryString["flag"] == "county")
            {
                int provinceId = MyUtils.ParseToInt(context.Request.QueryString["province"], 0);
                int cityId = MyUtils.ParseToInt(context.Request.QueryString["city"], 0);
                if (provinceId > 0 && cityId > 0)
                {
                    using (DBContext db = new DBContext())
                    {
                        DataTable dt = new DataTable();
                        SqlResult result = db.CallProcedure("usp_counties_get_by_proviceAndCity", provinceId, cityId);
                        if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                        {
                            dt = result.DataSet.Tables[0];
                        }
                        returnValue = "[";
                        foreach (DataRow dr in dt.Rows)
                        {
                            County county = new County { CountyID = MyUtils.ParseToInt(dr["id"], 0), CountyName = MyUtils.ParseToString(dr["countyname"], string.Empty) };
                            returnValue += UtilityMethod.ToJson(county) + ",";
                        }
                        if (returnValue.Length > 1)
                            returnValue = returnValue.Substring(0, returnValue.Length - 1);
                        returnValue += "]";
                    }
                }
            }
            context.Response.Write(returnValue);
            context.Response.Flush();
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}